package com.runnirr.doodles;


import java.util.Calendar;
import java.util.Date;

import com.runnirr.doodles.R;
import com.runnirr.doodles.Helpers.Utils;
import com.runnirr.doodles.Menu.MenuActions;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;

public class DoodleFilter extends Activity {

	private DatePicker datePicker;
	private Button viewDoodles;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		// Inflate our UI from its XML layout description.
		setContentView(R.layout.doodle_filter_layout);

		setUpActionIfAvailable();
		
		datePicker = (DatePicker) findViewById(R.id.filterDatePicker);
		viewDoodles = (Button) findViewById(R.id.submitFilterButton);
		
		int setYear= getIntent().getIntExtra("year", Calendar.getInstance().get(Calendar.YEAR));
		int setMonth = getIntent().getIntExtra("month", Calendar.getInstance().get(Calendar.MONTH)); //Already 0 based

		datePicker.updateDate(setYear, setMonth, 1);		// default to first of month. Day not used (removed)

		setDateLimitsIfAvailable();
		removeDayField();
	
		
		viewDoodles.setOnClickListener(new View.OnClickListener(){
			public void onClick(View v) {
				Intent intent = new Intent(DoodleFilter.this, DoodleList.class);
				intent.putExtra("year", datePicker.getYear());
				intent.putExtra("month", datePicker.getMonth() + 1); // Convert from 0 base to 1 base
				startActivity(intent);

			}
		});
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
	    MenuInflater inflater = getMenuInflater();
	    inflater.inflate(R.menu.doodle_menu, menu);
	    return true;
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		return MenuActions.getInstance().handleClick(item, this) ? true : super.onOptionsItemSelected(item);
	}
	
	@SuppressLint("NewApi")
	private void setUpActionIfAvailable(){
		if (Utils.isApi11Plus()){
			getActionBar().setDisplayHomeAsUpEnabled(false);
		}
	}
	
	@SuppressLint("NewApi")
	private void setDateLimitsIfAvailable(){
		if(Utils.isApi11Plus()){
			Calendar c = Calendar.getInstance();
			c.set(1999, Calendar.OCTOBER, 1); 		// October 1999
			c.getTimeInMillis();
			
			datePicker.setMinDate(c.getTimeInMillis());
			datePicker.setMaxDate(new Date().getTime());
		}
	}
	
	/**
	 * Remove day field from this.datePicker using reflection
	 * Copied from a StackOverflow post
	 * 	http://stackoverflow.com/questions/4122929/how-to-display-date-picker-for-android-with-only-month-and-year-fields
	 */
	private void removeDayField(){
		try {
		    java.lang.reflect.Field f[] = datePicker.getClass().getDeclaredFields();
		    for (java.lang.reflect.Field field : f) {
		        if (field.getName().equals("mDaySpinner") ||		// Honeycomb+ name 
		        		field.getName().equals("mDayPicker")) {		// Gingerbread name
		            field.setAccessible(true);
		            Object dayPicker = new Object();
		            dayPicker = field.get(datePicker);
		            ((View) dayPicker).setVisibility(View.GONE);
		        }
		    }
		} catch (Exception e) {
		    Log.d("ERROR", e.getMessage());
		}
		
	}
}
